package tjs.study.notes.mysql;

import java.sql.*;

/**
 * Created by Nicky.Tang on 2021/12/5 6:15 下午
 *
 * @since 02.12.10
 */
public class StreamQuery {

    public static void main(String[] args) throws Throwable {
        String sql = "select * from test";
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        Connection connection = getConn();
        try {
            pstmt = (PreparedStatement) connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            pstmt.setFetchSize(Integer.MIN_VALUE);
            resultSet = pstmt.executeQuery();
            int i = 0;
            while (resultSet.next()) {
                i++;
                System.out.println(resultSet.getString("c2"));
            }
            System.out.println("总计：" + i);
        } finally {
            connection.close();
        }

    }

    public static Connection getConn() {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/tjs_demo";
        String username = "root";
        String password = "qx123456";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


}
